package com.xauto.pioneer.appuser.activity.mine.user;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.MessageAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.MemberMessageInfoData;
import com.xauto.pioneer.appuser.model.PageInfo;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

import static com.xauto.pioneer.appuser.R.layout.layout_listview_empty;

/**
 * 消息列表 页面
 */

public class MemberMessageListActivity extends BaseActivity {


    private PromptDialog promptDialog;
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.layoutMessageStatus)
    View layoutMessageStatus;
    @Bind(R.id.message_list)
    PullToRefreshListView pullToRefreshListView;
    private ListView message_list;
    private List<MemberMessageInfoData> MemberMessageInfolist;
    private MessageAdapter adapter;
    //当前点击的消息相关内容
    MemberMessageInfoData MemberMessageInfoData;

    /**
     *
     * @param context
     */
    public static void startAction(Activity context){
        Intent intent = new Intent(context,MemberMessageListActivity.class);
        context.startActivityForResult(intent,1000);
    }
    @Override
    protected int getContentView() {
        return R.layout.ac_message_list;
    }
    @Override
    protected void onStart() {
        super.onStart();
        load();
    }
    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.user_message_list);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        promptDialog = new PromptDialog(mContext, getString(R.string.notice), getString(R.string.text_not_network),
                getString(R.string.to_setting), getString(R.string.logout),  getString(R.string.dialogpic_notice),new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去设置
                promptDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出
                promptDialog.dismiss();
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        message_list = pullToRefreshListView.getRefreshableView();
        message_list.setVerticalScrollBarEnabled(false);
        message_list.setDividerHeight(3);
        message_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //当前点击信息列表
                MemberMessageInfoData = adapter.getItem(position - 1);
                adapter.setSelectPosition(position-1);
                adapter.notifyDataSetChanged();
                MemberMessageDetailActivity.startAction(MemberMessageListActivity.this,MemberMessageInfoData.member_message_id,MemberMessageInfoData.member_message_date);
                        //message_details
                /*if(MemberMessageInfoData.link_route.equals("client_home")){
                    MemberMessageDetailActivity.startAction(MemberMessageListActivity.this,MemberMessageInfoData.member_message_date,MemberMessageInfoData.member_message_content);
                }
                if(MemberMessageInfoData.link_route.equals("member_aptitude")){
                    startActivity(new Intent(mContext, UsreAptitudeActivity.class));
                }
                if(MemberMessageInfoData.link_route.equals("phone")){
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+MemberMessageInfoData.link_value));
                    startActivity(intent);
                }
                if(MemberMessageInfoData.link_route.equals("member_deposit_pay")){
                    startActivity(new Intent(mContext, DepositBalanceActivity.class));
                }
                if(MemberMessageInfoData.link_route.equals("member_vouchlist_value")){
                    VoucherListActivity.startAction(MemberMessageListActivity.this,false,"",101, API.GET_VOUCHER_LIST);
                }*/
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //刷新
                page = 1;
                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    load();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                }
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new MessageAdapter(this, R.layout.item_message);
        message_list.setAdapter(adapter);
        page = 1;
        lHandler = new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                pullToRefreshListView.onRefreshComplete();
                JSONArray jsonArray = jsonStatus.data.optJSONArray("member_message_list");
                isMore = jsonStatus.pageInfo.more == 1 ? true : false;
                if (jsonArray != null && jsonArray.length() > 0) {
                    MemberMessageInfolist = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        MemberMessageInfolist.add(new MemberMessageInfoData().parse(jsonArray.optJSONObject(i)));
                    }
                    refreshList();
                } else {
                    if (page == 1)
                        showNoDataView();
                }
            }
        };
        //初始化数据
        load();
    }

    void refreshList() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (page == 1) {
                    adapter.getDataList().clear();
                }
                adapter.getDataList().addAll(MemberMessageInfolist);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void showNoDataView() {
        View emptyView = LayoutInflater.from(mContext).inflate(layout_listview_empty, null);
        ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("当前信息");
        //((ViewGroup) list.getParent()).addView(emptyView);
        //list.setEmptyView(emptyView);
        message_list.setVisibility(View.GONE);
        layoutMessageStatus.setVisibility(View.VISIBLE);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    void load() {
        message_list.setVisibility(View.VISIBLE);
        layoutMessageStatus.setVisibility(View.GONE);
        new LHttpLib().getMemberMessageList(mContext, memberId,"9",new PageInfo(page, Constants.COUNT), lHandler);
    }

    /*@OnClick({R.id.unused_text,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.unused_text:
                //setResult(RESULT_OK,new Intent().putExtra("voucherDetailInfo",voucherDetailInfo));
                finish();
                break;
        }
    }*/
}